package com.nondo.dean.order.controller.api.app;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.View;
import com.nondo.dean.order.domain.Cart;
import com.nondo.dean.order.model.param.ConfirmOrderParam;
import com.nondo.dean.order.model.param.MixCartParam;
import com.nondo.dean.order.model.result.AppCartResult;
import com.nondo.dean.order.model.result.ConfirmOrderResult;
import com.nondo.dean.order.service.CartService;

/**
 * 购物车管理APP接口
 * @author zlj
 * @date 2018年4月16日
 */
@RestController("apiAppCart")
@RequestMapping("/{v}/app/cart")
public class CartController {
    
    @Autowired
    CartService cartService;
    
    /**
     * 添加购物车
     * @param request
     * @param response
     * @param cart 购物车信息 {@link Cart}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Cart> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody Cart cart, BindingResult result) {
        View<Cart> view = new View<Cart>(20000, "添加成功");

        String message = null;
        
        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            message = this.cartService.addCart(cart);
            if (message != null){
                return View.of500Error(message);
            }
        }
        return view;
    }
    
    /**
     * 添加购物车
     * @param request
     * @param response
     * @param cart 购物车信息 {@link Cart}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/mix/add")
    public View<Cart> addMix(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody MixCartParam mixCart, BindingResult result) {
        View<Cart> view = new View<Cart>(20000, "添加成功");

        String message = null;
        
        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            List<Cart> cartList = mixCart.getCartList();
            for (Cart cart : cartList) {
                message = this.cartService.addCart(cart);
                if (message != null) {
                    return View.of500Error(message);
                }
            }
        }
        return view;
    }

    /**
     * 更新购物车
     * @param request
     * @param response
     * @param cart 购物车信息 {@link Cart}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Cart> update(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody Cart cart, BindingResult result) {
        View<Cart> view = new View<Cart>(20000, "更新成功");

        String message = null;
        
        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Cart cartDb = this.cartService.selectCart(cart.getId());
            BeanUtils.copy(cart, cartDb);
            message = this.cartService.updateCart(cartDb);
            if (message != null){
                return View.of500Error(message);
            }
        }

        return view;
    }

    /**
     * 删除购物车
     * @param request
     * @param response
     * @param ids
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{ids}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("ids") String ids) {
        View view = new View(20000, "删除成功");

        for (String id : ids.split(",")) {
            Cart cart = cartService.selectCart(Integer.parseInt(id));
            cart.setDelFlag(Cart.DEL_FLAG_DELETE);
            this.cartService.updateCart(cart);
        }

        return view;
    }
    
    /**
     * 获取购物车列表
     * @param request
     * @param response
     * @return {@link AppCartResult}
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<AppCartResult> list(HttpServletRequest request, HttpServletResponse response) {
        View<AppCartResult> view = new View<AppCartResult>(20000, "获取列表成功");

        AppCartResult cartResult = cartService.getCartResult(null);
        
        view.setData(cartResult);
        
        return view;
    }
    
    
    /**
     * 获取确认订单数据(立即购买或购物车结算)
     * @param request
     * @param response
     * @param {{@link ConfirmOrderParam}
     * @return {@link ConfirmOrderResult}
     */
    @ApiVersion(1)
    @RequestMapping("/confirm")
    public View<ConfirmOrderResult> confirmOrder(HttpServletRequest request, HttpServletResponse response, @RequestBody ConfirmOrderParam orderParam) {
        View<ConfirmOrderResult> view = new View<ConfirmOrderResult>(20000, "获取列表成功");

        String message = ConfirmOrderParam.checkeInputParam(orderParam);
        
        if (message != null) {
            return View.of404Error(message);
        }
        
        ConfirmOrderResult confirmOrder = cartService.getConfirmOrderDetail(orderParam);
        
        view.setData(confirmOrder);
        
        return view;
    }
}